﻿Imports System.Security.Cryptography
Imports System.IO

Imports System.Text

Public Class main

    Dim f As FileStream
    
    Private Sub openFileButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openFileButton.Click
        Dim fdlg As OpenFileDialog = New OpenFileDialog()
        fdlg.Title = "Which File?"
        fdlg.InitialDirectory = "c:\"
        fdlg.Filter = "All Files (*.*)|*.*|All Files (*.*)|*.*"
        fdlg.FilterIndex = 2
        fdlg.RestoreDirectory = True
        If fdlg.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = fdlg.FileName
        End If
        getHash.Enabled() = True
    End Sub

    Private Sub getHash_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles getHash.Click
        f = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192) 'opens the file
        Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider 'imports the md5 hasher
        md5Obj.ComputeHash(f)  'creates the hash
        Dim hash() As Byte = md5Obj.Hash  'defines the hash
        Dim sb As New System.Text.StringBuilder(hash.Length * 2)  'starts to build the human readable hash
        For i As Integer = 0 To hash.Length - 1
            sb.Append(hash(i).ToString("X2"))
        Next
        hashText.Text = sb.ToString().ToLower()   'outputs the hash to a text box with lowercase letters
        f.Close()
        datFile.Enabled() = True
    End Sub

    Private Sub datFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles datFile.Click
        Dim file As String = "hashes.dat"
        Dim o As StreamWriter = New StreamWriter(file, True)
        Dim text As String = TextBox1.Text & "     " & hashText.Text & Environment.NewLine
        o.Write(text)
        o.Close()
    End Sub
End Class
